﻿using System;
using System.Collections.Generic;
using Omniscient.Foundation.Contrib.Data.MySql;
using Omniscient.Foundation.Data;
using MySql.Data.MySqlClient;
using Omniscient.Foundation.ApplicationModel;

namespace Omniscient.TimeTracker.Data.Adapters.MySql
{
    public class ResourceAdapter : IEntityAdapter<Resource>
    {
        IConnectivity _connectivity;

        public ResourceAdapter() : this(ApplicationManager.Current.ServiceProvider.GetService<IConnectivity>()) { }

        public ResourceAdapter(IConnectivity connectivity)
        {
            if (connectivity == null) throw new ArgumentNullException("The adapter cannot work with no connectivity.  Null argument: connectivity.");
            _connectivity = connectivity;
        }

        #region IEntityAdapter<Resource> Members

        public EntityList<Resource> LoadAll()
        {
            MySqlConnection cn;
            MySqlCommand cmd;
            MySqlDataReader rd;
            EntityList<Resource> resources;

            resources = new EntityList<Resource>();
            cn = _connectivity.CreateConnection();

            string sql = @"SELECT ResourceGuid, UserName, Password, RealName, Email, IsAdmin FROM resource";

            try
            {
                cmd = cn.CreateCommand();
                cmd.CommandText = sql;
                cn.Open();
                rd = cmd.ExecuteReader();
                while (rd.Read())
                {
                    Resource r;
                    Guid id;

                    id = new Guid(rd.GetString("ResourceGuid"));
                    r = new Resource(id, true);

                    r.UserName = rd.GetString("UserName");
                    r.Password = rd.GetString("Password");
                    r.PersonName = rd.GetString("RealName");
                    r.Email = rd.GetString("Email");
                    r.IsAdmin = rd.GetBoolean("IsAdmin");

                    resources.Add(r);
                }
                rd.Close();
                return resources;
            }
            finally
            {
                cn.Dispose();
            }
        }

        public EntityList<Resource> LoadByForeignKey(string propertyName, Guid id)
        {
            throw new NotImplementedException();
        }

        public Resource LoadByKey(Guid id)
        {
            throw new NotImplementedException();
        }

        public EntityList<Resource> LoadByObjectQuery(Omniscient.Foundation.Data.ObjectQuery.OQuery<Resource> query)
        {
            throw new NotImplementedException();
        }

        public EntityList<Resource> LoadByQuery(string queryName)
        {
            throw new NotImplementedException();
        }

        public EntityList<Resource> LoadByValueProperty(string propertyName, object value)
        {
            throw new NotImplementedException();
        }

        public void Save(IEnumerable<Resource> entities)
        {
            throw new NotImplementedException();
        }

        public void Save(Resource entity)
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}
